<?php

require_once ROOT_APPLICATION_MODULES_TEMPLATE_FORMS.DS."Banner.php";
require_once ROOT_APPLICATION_MODULES_TEMPLATE_OBJECTS.DS."Banner.php";
require_once ROOT_APPLICATION_MODULES_TEMPLATE_MODELS.DS."Banner.php";

class IndexController extends Zend_Controller_Action
{
	
    public function init()
    {
        /* Initialize action controller here */
    }
    
    public function addAction()
    {   
    	//Create form enter info for new Banner
    	$form = new Application_Form_Banner();
    	
    	//Set label for the button
        $form->submit->setLabel('Add');
        
        //Set client view page form  = $form
        $this->view->form = $form;
        
        //If can get client request 
        if ($this->getRequest()->isPost()) 
        {
        	//Get data from form.
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData))   //If data is valid 
            {
            	//Get data one by one
                $name = $form->getValue('name');
                $text = $form->getValue('text');
                $num_clicks = $form->getValue('num_clicks');
                $created_date = $form->getValue('created_date');
                $start_date = $form->getValue('start_date');
                $expired = $form->getValue('expired_date');
                $client_id = (int)$form->getValue('client_id');
                $code = $form->getValue('code');
                $click_url = $form->getValue('click_url');
                $target = $form->getValue('target');
                $format = $form->getValue('format');
                $image_url = $form->getValue('image_url');
                $ordering = (int)$form->getValue('ordering');
                $mode = $form->getValue('mode');
                $timeout = (int)$form->getValue('timeout');
                $status = $form->getValue('status');
                
                //Create new Page object from datas
                $banner = new Banner(null, $name, $text, $num_clicks,$created_date,$start_date,$expired, $client_id,
                						$code,$click_url,$target,$format,$image_url, $ordering, $mode, $timeout, $status);
                
                //Create Banner model 
                $banner = new End_Models_Banner();
                
                //Use insert function from Banner model
                $banners->_insert($banners);
                
                //Move to index page
                $this->_helper->redirector('index');
            } else 
            {
            	//Return form
                $form->populate($formData);
            }
        }
    }
    
    public function deleteAction()
    {	
    	if ($this->getRequest()->isPost()) 
    	{
            $del = $this->getRequest()->getPost('del'); 
    		if ($del == 'Yes') 
            {
                $banner_id = (int)$this->getRequest()->getPost('banner_id');
                $banners = new End_Models_Banner();
                $banners->_delete($banner_id);
            }
            $this->_helper->redirector('index');
        } else 
        {
            $banner_id = $this->_getParam('banner_id', 0);
            $banners = new End_Models_Banner();
            $this->view->page = Utility_Converter::objectToArray($banners->_find($banner_id));
        }
    }
    
    public function editAction()
    {
    	$form = new Application_Form_Page();
        $form->submit->setLabel('Save');
        $this->view->form = $form;
       
        if ($this->getRequest()->isPost()) 
        {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) 
            {
                $banner_id = (int)$form->getValue('banner_id');
                $name = $form->getValue('name');
                $text = $form->getValue('text');
                $num_clicks = $form->getValue('num_clicks');
                $created_date = $form->getValue('created_date');
                $start_date = $form->getValue('start_date');
                $expired = $form->getValue('expired_date');
                $client_id = (int)$form->getValue('client_id');
                $code = $form->getValue('code');
                $click_url = $form->getValue('click_url');
                $target = $form->getValue('target');
                $format = $form->getValue('format');
                $image_url = $form->getValue('image_url');
                $ordering = (int)$form->getValue('ordering');
                $mode = $form->getValue('mode');
                $timeout = (int)$form->getValue('timeout');
                $status = $form->getValue('status');
                
				$banner = new Banner(null, $name, $text, $num_clicks,$created_date,$start_date,$expired, $client_id,
                						$code,$click_url,$target,$format,$image_url, $ordering, $mode, $timeout, $status);	//dua thong tin vao doi tuong
                $banners = new End_Models_Banner();						//tao doi tuong model voi cac phuong thuc xu li doi tuong o tren
                $banners->_update($banner);								//xu li doi tuong voi phuong thuc cua model
                $this->_helper->redirector('index');				//chuyen den trang index
            } else 
            {
                $form->populate($formData);
            }
        } else 
        {
            $banner_id = $this->_getParam('banner_id', 0);
            if ($banner_id > 0) 
            {
                $banners = new End_Models_Banner();
                $form->populate(Utility_Converter::objectToArray($banners->_find($banner_id)));
            }
        }
    }
    
    public function indexAction()
    {
    	$modelBanner = new End_Models_Banner();
		$banner = $modelBanner->fetchAll();
		$this->view->Banner = $banner;
    }
    
}


